package com.huxuesong.prcc.util.submail.utils;

import com.huxuesong.prcc.util.submail.config.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

/**
 * 这个类通过加载app_config.properties文件创建配置对象并获取值，包括创建
 * MailConfig，MessageConfig，VoiceConfig,InternationalsmsConfig,MobiledataConfig
 * @see AppConfig
 * @see MailConfig
 * @see MessageConfig
 * @see VoiceConfig
 * @see InternationalsmsConfig
 * @see MobiledataConfig
 * @author submail
 *
 */
@Component
public class ConfigLoader {

	@Autowired
	private Environment environment;


	/**
	 * 加载文件时，类载入，静态块内部的操作将被运行一次
	 * */


	/**
	 * enum define two kinds of configuration.
	 * */
	public static enum ConfigType {
		Mail, Message,Voice,Internationalsms,Mobiledata
	};

	/**
	 * 外部类的静态方法，可以通过加载文件创建配置。
	 * 
	 * @param type
	 *            ConfigType
	 * @return If the type is ConfigType#Mail,return the instance of
	 *         {@link MailConfig}. And,if the type is ConfigType#Message,return
	 *         the instance of {@link MessageConfig}.
	 * */
	public  AppConfig load(ConfigType type) {
		switch (type) {
		case Mail:
			return createMailConfig();
		case Message:
			return createMessageConfig();
		case Voice:
			return createVoiceConfig();
		case Internationalsms:
			return createInternationalsmsConfig();
		case Mobiledata:
			return createMobiledataConfig();
		default:
			return null;
		}
	}

	private  AppConfig createMailConfig() {
		AppConfig config = new MailConfig();
		config.setAppId(environment.getProperty(MailConfig.APP_ID));
		config.setAppKey(environment.getProperty(MailConfig.APP_KEY));
		config.setSignType(environment.getProperty(MailConfig.APP_SIGNTYPE));
		return config;
	}

	private  AppConfig createMessageConfig() {
		AppConfig config = new MessageConfig();
		config.setAppId(environment.getProperty(MessageConfig.APP_ID));
		config.setAppKey(environment.getProperty(MessageConfig.APP_KEY));
		config.setSignType(environment.getProperty(MessageConfig.APP_SIGNTYPE));
		return config;
	}
	
	private  AppConfig createVoiceConfig() {
		AppConfig config = new VoiceConfig();
		config.setAppId(environment.getProperty(VoiceConfig.APP_ID));
		config.setAppKey(environment.getProperty(VoiceConfig.APP_KEY));
		config.setSignType(environment.getProperty(VoiceConfig.APP_SIGNTYPE));
		return config;
	}
	
	private  AppConfig createInternationalsmsConfig() {
		AppConfig config = new InternationalsmsConfig();
		config.setAppId(environment.getProperty(InternationalsmsConfig.APP_ID));
		config.setAppKey(environment.getProperty(InternationalsmsConfig.APP_KEY));
		config.setSignType(environment.getProperty(InternationalsmsConfig.APP_SIGNTYPE));
		return config;
	}
	
	private  AppConfig createMobiledataConfig() {
		AppConfig config = new MobiledataConfig();
		config.setAppId(environment.getProperty(MobiledataConfig.APP_ID));
		config.setAppKey(environment.getProperty(MobiledataConfig.APP_KEY));
		config.setSignType(environment.getProperty(MobiledataConfig.APP_SIGNTYPE));
		return config;
	}

}
